package historymanager.model;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * Object-Model-Class for ControllerUnit
 * 
 * @author Thomas Aulich
 * @version 1.0
 *
 */
public class MeasurePoint {
	private ControllerUnit controllerUnit;
	private URL readValUrl;
	private long id;
	private String symbolName;
	private String type;
	private int registerAddress;
	private String aliasName;
	private String format;
	private int decimalDigits;
	private String unit;
	private int active;

	public MeasurePoint(long pId, ControllerUnit pControllerUnit,
            String pSymbolName, String pAliasName, String pType,
            int pRegisterAddress, String pFormat, int pDecimalDigits,
            String pUnit)
			throws MalformedURLException {
		id=pId;
		controllerUnit = pControllerUnit;
		symbolName = pSymbolName;
		type = pType;
		registerAddress = pRegisterAddress;
		aliasName = pAliasName;
		format = pFormat;
		decimalDigits = pDecimalDigits;
		unit = pUnit;
		active = 1; //Defaultvalue = true; Otherwise use setter!

		// Anfrage Register 1000 zu lesen, Wert in dezimalem Format:
		// http://pc_name/station_name/cgi-bin/readVal.exe?PDP,,R1000,d
		   readValUrl = new URL(controllerUnit.getReadValUrl() + "?" + "PDP,,"+type + registerAddress + ","
					+ format);
		
	}

	public long getServerID() {
		return controllerUnit.getId();
	}

	public long getId() {
		return id;
	}
	
	public int getDecimalDigits() {
		return decimalDigits;
	}

	public URL getReadValUrl() {
		return readValUrl;
	}

	public static double getDoubleValue(MeasurePoint pMeasurePoint, long pValue) {
		double decimalDigits = pMeasurePoint.getDecimalDigits();
		double factor = Math.pow(10, decimalDigits);
		double result = pValue / factor;
		return (result);
	}

	public String getSymbolName() {
		return symbolName;
	}

	public String getAliasName() {
		return aliasName;
	}

	public String getDescription() {
		
		  return "Station: " + controllerUnit.getStationName() + "; Alias: " +
		  controllerUnit.getStationAlias() + ";Symbolname: " + symbolName +
		  "; Can be reached for 'readval' at: " + getReadValUrl();
	}

	public ControllerUnit getControllerUnit() {
		return controllerUnit;
	}

	public String getType() {
		return type;
	}

	public String getUnit() {
		return unit;
	}
	
	public int getActive() {
		return active;
	}

	public void setActive(int active) {
		this.active = active;
	}

}
